package com.guoxin.chapter2.deadlocktest;

/**
 * @author guoxin
 * @date 2019年8月19日
 */
public class DeadThread extends Thread {

	private String username;
	private Object lock1 = new Object();
	private Object lock2 = new Object();

	public void setFlag(String username) {
		this.username = username;
	}

	@Override
	public void run() {
		if ("a".equals(username)) {
			synchronized (lock1) {
				try {
					System.out.println("username=" + username);
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (lock2) {
					System.out.println(" 按 lock1->lock2 代码顺序执行了");
				}
			}
		}

		if ("b".equals(username)) {
			synchronized (lock2) {
				try {
					System.out.println("username=" + username);
					Thread.sleep(3000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				synchronized (lock1) {
					System.out.println(" 按 lock2->lock1 代码顺序执行了");
				}
			}
		}
	}
}
